//
//  TrainManagementCell.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/12.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TrainManagementCell.h"
@interface TrainManagementCell()
@property (nonatomic,weak) UILabel *timeLabel;
@property (nonatomic,weak) UILabel *stateLabel;
@property (nonatomic,strong) UIImageView *shadowimgView;
@end

@implementation TrainManagementCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *timeLabel = [self returnLabelWithFont:LoginFont-3 WithColor:[UIColor colorWithHexString:@"#666666"]];
    _timeLabel = timeLabel;
    [self.contentView addSubview:self.timeLabel];
    
    UILabel *stateLabel = [self returnLabelWithFont:LoginFont-3 WithColor:[UIColor colorWithHexString:@"#21d4a4"]];
    _stateLabel = stateLabel;
    [self.contentView addSubview:self.stateLabel];
    
    UIButton *actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [actionBtn addTarget:self action:@selector(actionChange:) forControlEvents:UIControlEventTouchUpInside];
    _actionBtn = actionBtn;
    [self.contentView addSubview:actionBtn];
    
    self.shadowimgView = [[UIImageView alloc] init];
    self.shadowimgView.image = [UIImage imageNamed:@"TrainManagementOverdue"];
    self.shadowimgView.backgroundColor = [UIColor clearColor];
    
    //布局
    WS(ws);
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws).with.offset(35*SCALEUNIT);
        make.centerY.mas_equalTo(ws);
        make.height.mas_equalTo(ws).multipliedBy(0.5);
        make.width.mas_equalTo(195*SCALEUNIT);
    }];
    [self.stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.timeLabel.mas_right);
        make.centerY.height.mas_equalTo(ws.timeLabel);
        make.width.mas_equalTo(280*SCALEUNIT);
    }];
    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ws.stateLabel);
        make.right.mas_equalTo(ws).with.offset(-35*SCALEUNIT);
        make.size.mas_equalTo(CGSizeMake(162*SCALEUNIT, 64*SCALEUNIT));
    }];
}

- (UILabel *)returnLabelWithFont:(NSInteger)fontSize WithColor:(UIColor *)labelColor{
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.textAlignment = NSTextAlignmentLeft;
    tempLabel.font = [UIFont systemFontOfSize:fontSize];
    tempLabel.textColor = labelColor;
    return tempLabel;
}

#pragma mark ==========取消+恢复==========
- (void)actionChange:(UIButton *)btn{
    btn.userInteractionEnabled = NO;
    if ([self.delegate respondsToSelector:@selector(trainManagementCellDelegateWithMessage:WithBtn:)]) {
        [self.delegate trainManagementCellDelegateWithMessage:self.dataArr WithBtn:btn];
    }
}

#pragma mark ==========赋值==========
- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    NSString *normalState;
    NSString *selectState;
    if ([[[_dataArr objectAtIndex:0] objectForKey:@"state"] integerValue]==3) {
        self.actionBtn.userInteractionEnabled = NO;
        normalState = @"TrainManagementOverdue";
        selectState = @"TrainManagementOverdue";
    } else {
        NSArray *timeArr = [[[_dataArr objectAtIndex:0] objectForKey:@"name"] componentsSeparatedByString:@"-"];
        if ([self judgeTimeByStartAndEnd:[timeArr objectAtIndex:0] withExpireTime:[timeArr objectAtIndex:1]] && [self compareOneDay:[_dataArr objectAtIndex:2]]==0) {
            if (self.actionBtn.userInteractionEnabled==YES) self.actionBtn.userInteractionEnabled = NO;
            normalState = @"TrainManagementCancle_Overdue";
            selectState = @"TrainManagementRecovery_Overdue";
        } else {
            if (self.actionBtn.userInteractionEnabled==NO) self.actionBtn.userInteractionEnabled = YES;
            normalState = @"TrainManagementCancle";
            selectState = @"TrainManagementRecovery";
        }
    }
    [self.actionBtn setBackgroundImage:[UIImage imageNamed:normalState] forState:UIControlStateNormal];
    [self.actionBtn setBackgroundImage:[UIImage imageNamed:selectState] forState:UIControlStateSelected];
    self.timeLabel.text = [[_dataArr objectAtIndex:0] objectForKey:@"name"];
    self.stateLabel.text = [[[_dataArr objectAtIndex:0] allKeys] containsObject:@"reason"]?[[_dataArr objectAtIndex:0] objectForKey:@"reason"]:@"正常";
    if ([self compareOneDay:[_dataArr objectAtIndex:2]]==2) {
        if (self.actionBtn.hidden==NO) self.actionBtn.hidden = YES;
        self.shadowimgView.frame = self.actionBtn.frame;
        [self addSubview:self.shadowimgView];
    } else {
        [self.shadowimgView removeFromSuperview];
        if (self.actionBtn.hidden==YES) self.actionBtn.hidden = NO;
    }
    self.actionBtn.selected = [[[_dataArr objectAtIndex:0] objectForKey:@"state"] integerValue]==1?NO:YES;
    self.stateLabel.textColor = [self compareOneDay:[_dataArr objectAtIndex:2]]==2?[UIColor colorWithRed:200.0/250.0 green:200.0/250.0 blue:200.0/250.0 alpha:1]:[[[_dataArr objectAtIndex:0] objectForKey:@"state"] integerValue]==1?[UIColor colorWithHexString:@"#21d4a4"]:[[[_dataArr objectAtIndex:0] objectForKey:@"state"] integerValue]==2?[UIColor colorWithHexString:@"#ff4949"]:[UIColor colorWithRed:200.0/250.0 green:200.0/250.0 blue:200.0/250.0 alpha:1];
}

#pragma mark ==========判断当前日期是否过期( 0相等,返回1没过期,2已过期 )==========
- (NSInteger)compareOneDay:(NSString *)oneDay{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *anotherDayStr = [dateFormatter stringFromDate:[NSDate date]];
    NSDate *dateA = [dateFormatter dateFromString:oneDay];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedDescending) {
        //在指定时间之后 没过期
        return 1;
    }
    else if (result == NSOrderedAscending){
        //在指定时间之前 已过期
        return 2;
    }
    //刚好时间一样.
    return 0;
}

#pragma mark ==========判断当前时间是否在某个时间段内==========
- (BOOL)judgeTimeByStartAndEnd:(NSString *)startTime withExpireTime:(NSString *)expireTime {
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // 时间格式,此处遇到过坑,建议时间HH大写,手机24小时进制和12小时禁止都可以完美格式化
    [dateFormat setDateFormat:@"HH:mm"];
    NSString * todayStr=[dateFormat stringFromDate:today];//将日期转换成字符串
    today=[ dateFormat dateFromString:todayStr];//转换成NSDate类型。日期置为方法默认日期
    //startTime格式为 02:22   expireTime格式为 12:44
    NSDate *start = [dateFormat dateFromString:startTime];
    NSDate *expire = [dateFormat dateFromString:expireTime];
    
    if ([today compare:start] == NSOrderedDescending && [today compare:expire] == NSOrderedAscending) {
        return YES;
    }
    return NO;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



@end
